package net.jiastudy.biz.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import net.jiastudy.biz.sys.entity.Attachment;
import net.jiastudy.biz.sys.entity.AttachmentFile;
import net.jiastudy.biz.sys.entity.File;
import net.jiastudy.biz.sys.entity.FileTarget;
import net.jiastudy.biz.sys.mapper.AttachmentFileMapper;
import net.jiastudy.biz.sys.mapper.AttachmentMapper;
import net.jiastudy.biz.sys.service.AttachmentService;
import net.jiastudy.biz.sys.service.FileService;
import net.jiastudy.core.base.BaseService;
import net.jiastudy.core.utils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
@Transactional(rollbackFor = Exception.class)
public class AttachmentServiceImpl extends BaseService<AttachmentMapper, Attachment> implements AttachmentService {

    @Resource
    private FileService fileService;
    @Resource
    private AttachmentFileMapper attachmentFileMapper;

    @Override
    public Long create(Set<Long> fileIds, FileTarget fileTarget) {
        if (null == fileIds) {
            return null;
        }
        Attachment attachment = new Attachment();
        attachment.setTarget(fileTarget);
        this.insert(attachment);
        for (Long fileId : fileIds) {
            AttachmentFile entity = new AttachmentFile();
            entity.setAttachmentId(attachment.getId());
            entity.setFileId(fileId);
            attachmentFileMapper.insert(entity);
        }
        return attachment.getId();
    }

    @Override
    public Long update(Long id, Set<Long> fileIds) {
        Wrapper<AttachmentFile> wrapper = new EntityWrapper<>();
        wrapper.eq("attachment_id", id);
        attachmentFileMapper.delete(wrapper);
        if (null != fileIds) {
            for (Long fileId : fileIds) {
                AttachmentFile entity = new AttachmentFile();
                entity.setAttachmentId(id);
                entity.setFileId(fileId);
                attachmentFileMapper.insert(entity);
            }
        }

        return id;
    }

    @Override
    public Long addFile(Long id, Set<Long> fileIds) {
        for (Long fileId : fileIds) {
            AttachmentFile entity = new AttachmentFile();
            entity.setAttachmentId(id);
            entity.setFileId(fileId);
            attachmentFileMapper.insert(entity);
        }

        return id;
    }

    @Override
    public boolean delete(Long id) {
        if (null == id) {
            return true;
        }
        Wrapper<AttachmentFile> wrapper = new EntityWrapper<>();
        wrapper.eq("attachment_id", id);
        attachmentFileMapper.delete(wrapper);
        // TODO 删除文件
        return this.deleteById(id);
    }

    @Override
    public List<File> findFildsByAttachmentId(Long id) {
        if (null == id) {
            return null;
        }
        Wrapper<AttachmentFile> wrapper = new EntityWrapper<>();
        wrapper.eq("attachment_id", id);
        List<AttachmentFile> attachmentFiles = attachmentFileMapper.selectList(wrapper);
        Set<Long> fileIds = (Set<Long>) BeanUtils.getFiled(attachmentFiles, "fileId", Long.class);
        return fileService.findByIds(fileIds);
    }

    @Override
    public File getLatestFileByAttachmentId(Long id) {
        if (null == id) {
            return null;
        }
        File file = new File();
        Wrapper<AttachmentFile> wrapper = new EntityWrapper<>();
        wrapper.eq("attachment_id", id);
        wrapper.orderBy("create_date", false);
        List<AttachmentFile> attachmentFiles = attachmentFileMapper.selectList(wrapper);
        if (null != attachmentFiles && attachmentFiles.size() > 0) {
            Long fileId = attachmentFiles.get(0).getFileId();
            file = fileService.get(fileId);
        }

        return file;
    }
}
